package com.java.stream;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamTest01 {

    static void doTest01(){
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        long count = integers.stream() //初始操作(获取Stream对象)
                .filter(( integer)-> integer%2==0)//中间操作
                .count();//终止操作
        System.out.println(count);
    }
    static void doTest02(){
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> collect = integers.stream() //初始操作(获取Stream对象)
                .filter((integer) -> integer % 2 == 0)//中间操作
                .collect(Collectors.toList());
        System.out.println(collect);
    }
    static void doTest03(){
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        int sum = integers.parallelStream() //获取并行流对象
                //.mapToInt((i)->i.intValue())
                .mapToInt(Integer::intValue) //中间操作(将Integer类型转换为int类型)
                .sum();//终止操作(求和)
        System.out.println(sum);
    }
    static void doTest04(){
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
         integers.stream() //初始操作(获取Stream对象)
                .filter((integer) -> integer % 2 == 0)//中间操作
                .forEach((item)->System.out.println(item));//终止操作
                //.forEach(System.out::println);//终止操作,可用于替换上面语句
    }
    static void doTest05(){
        //1.Stream对象创建
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5,2, 6, 6, 7, 8, 9, 10,10);
        //2.中间操作
        stream.filter(item->item%2==0)
              .skip(1) //跳过几个
              .limit(4) //获取几个
              .distinct() //去重
              .sorted() //排序
        //3.终止操作
              .forEach(System.out::println);
    }
    static void doTest06(){
        class Employee{
            int id;
            int salary;
            public Employee(int id,int salary){
                this.id=id;
                this.salary=salary;
            }
            @Override
            public String toString() {
                return "Employee{" +
                        "id=" + id +
                        ", salary=" + salary +
                        '}';
            }
        }
        Stream<Employee> stream=
                Stream.of(new Employee(1,2000),new Employee(2,1000));
        stream.sorted((e1,e2)->{return e1.salary-e2.salary;})
              .forEach(System.out::println);
    }
    static void doTest07(){
        Stream<String> stream = Arrays.stream(new String[]{"A", "AB", "ABC", "ABCD"});
        stream.filter(item->item.length()>=3)
              .map(item->item.toLowerCase())
              .forEach(System.out::println);
    }

    public static void main(String[] args) {
        doTest07();
    }
}
